package com.yyplatform.frontend.infra.utils;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-14
 */
@Slf4j
public class DateUtils {
    private static final int YEARS = 10;


    public static Date getBeginDate(String[] arr) {
        if (arr == null || arr.length == 0) {
            return getBeginDate("");
        }
        return getBeginDate(arr[0]);
    }

    public static Date getEndDate(String[] arr) {
        if (arr == null || arr.length < 2) {
            return getEndDate("");
        }
        return getEndDate(arr[1]);
    }


    public static Date getBeginDate(String beginStr) {
        Date now = new Date();
        if (StringUtils.isEmpty(beginStr)) {
            return org.apache.commons.lang3.time.DateUtils.addYears(now, -YEARS);
        }

        try {
            return org.apache.commons.lang3.time.DateUtils.parseDate(beginStr, "yyyy-MM-dd");
        } catch (Exception ex) {
            log.error("DateUtils.getBeginDate error, beginStr:{}", beginStr, ex);
        }
        return org.apache.commons.lang3.time.DateUtils.addYears(now, -YEARS);
    }

    public static Date getEndDate(String endStr) {
        Date now = new Date();
        if (StringUtils.isEmpty(endStr)) {
            return org.apache.commons.lang3.time.DateUtils.addYears(now, YEARS);
        }

        try {
            return org.apache.commons.lang3.time.DateUtils.parseDate(endStr, "yyyy-MM-dd");
        } catch (Exception ex) {
            log.error("DateUtils.getEndDate error, endStr:{}", endStr, ex);
        }

        return org.apache.commons.lang3.time.DateUtils.addYears(now, YEARS);
    }

}
